package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.union.data.InsDataBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.InsuredBean;
import com.focus.xyz.union.pages.backend.BackMenuList;
import com.focus.xyz.union.pages.backend.BackNewOrderPage;
import com.focus.xyz.union.pages.frontend.FrontMenuList;
import com.focus.xyz.union.pages.frontend.WebsiteDetailPage;
import com.focus.xyz.union.pages.frontend.tables.OrderTable;
import com.focus.xyz.union.pages.frontend.tables.ProxyProdTable;
import com.focus.xyz.union.pages.frontend.tables.WebSiteTable;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/26.
 */
public class AllChannelToubaoTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(AllChannelToubaoTest.class);

    private WebDriver driver;
    private KeyWordAction action;

    @BeforeClass(description = "开始之前清理一下cookie")
    private void beforeClass() throws Exception{
        action.DelUnionFrontCookie();
    }

    @Parameters({"nodeUrl","browser","userName","password"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String userName,String password) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
//        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        super.init(driver);
        action = new KeyWordAction(driver);
        action.LoginFront(userName,password);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }


    @Parameters({"insDataIdx","payPass"})
    @Test(description = "代理人从代理产品投保")
    public void AGENT_TOUBAO(String insDataIdx,String payPass) throws Exception{
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_TB_MENU);
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        ProxyProdTable ptable = new ProxyProdTable(driver,action);
        ptable.linkDetail(bean.getProdName());
        action.pause(2000l);//打开新页面，正常等待时间
        action.CommonToubao(bean);
        action.pause(2000l);
        action.AcctPay(bean.getExpectedPrice(),payPass);
    }

    @Parameters({"insDataIdx"})
    @Test(description = "网站主从所属频道投保")
    public void WEBMASTER_TOUBAO(String insDataIdx) throws Exception{
        //点击“频道接入”菜单
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_SITE_MENU);
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        //找到网站，查看频道
        WebSiteTable webSiteTable = new WebSiteTable(driver,action);
        String status = webSiteTable.getStatus("测试频道-勿删");
        if(StringUtils.isNotBlank(status)&&"已审核".equals(status)){
            webSiteTable.linkDetail("测试频道-勿删");
            WebsiteDetailPage wd = new WebsiteDetailPage(driver,action);//sid=npjtdh0RxRDK
            wd.linkProdDetail(bean.getProdName());
            action.pause(1000l);
            action.CommonToubao(bean);
            action.pause(2000l);
            action.CommonUnionpay();
        }
    }

    @Task(taskName = "DEL_UNION_ORDER_APP.sql",paras = {"param0=cimon010"})
    @Parameters({"agentName","agentPass","userName","insDataIdx"})
    @Test(description = "客服后台代客投保")
    public void CUSTOMER_SERVICE_AGENT_TOUBAO(String agentName,String agentPass,String userName,String insDataIdx) throws Exception{
        try{
            action.LogoutFront();
            action.LoginBack(agentName,agentPass);
            BackMenuList menuList = new BackMenuList(driver,action);
            menuList.openExpandMenu("客服代理投保","新建订单");
            BackNewOrderPage bp = new BackNewOrderPage(driver,action);
            InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
            bp.toubao(userName,bean);
            action.pause(3000l);
            /*校验投保信息*/
            action.assertStringTrue(bean.getProdName());
            List<InsuredBean> listib= InsDataService.getInsuredById(bean.getInsuredId().split(","));
            for(InsuredBean ib : listib){
                action.assertStringTrue(ib.getDev_name());//验证订单信息中的被保人姓名
            }
        }catch (Exception e){
            throw new Exception(e);
        }
    }


    @Parameters({"insDataIdx"})
    @Test(description = "客服后台代客投保完成之后，客户登录确认订单信息、支付")
    public void CUSTOMER_SERVICE_AGENT_TOUBAO_CHECK(String insDataIdx) throws Exception{
        FrontMenuList fm = new FrontMenuList(driver,action);
        fm.openMenu(Constants.U_F_ORD_MENU);
        OrderTable ot = new OrderTable(driver,action);
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        String ordStatus = ot.getOrderStatus(bean.getProdName());
        /*校验待提交状态*/
        if(!ordStatus.equals("待提交")){
            action.fail("客服后台代客下单的订单状态不正确。");
        }
        /*确认订单校验*/
        ot.confirmOrder();
        action.switchToParentWindow("http://union.xyz.cn/ordertrans/orderlist");
        action.refresh();
        /*校验待支付状态*/
        ordStatus = ot.getOrderStatus(bean.getProdName());
        if(!ordStatus.equals("待支付")){
            action.fail("客户确认订单后订单状态不正确。");
        }
        action.switchToParentWindow("http://union.xyz.cn/unionPay/pay");
        /*支付校验*/
        ot.pay("ins123");
        action.assertStringTrue("支付成功");
        action.switchToParentWindow("http://union.xyz.cn/ordertrans/orderlist");
        action.refresh();
        /*校验已支付状态*/
        ordStatus = ot.getOrderStatus(bean.getProdName());
        if(!ordStatus.equals("已支付")){
            action.fail("客户支付成功后，订单状态显示不正确。");
        }
    }


    @Task(taskName = "DEL_UNION_ORDER_APP.sql",paras = {"param0=cimon010"})
    @Test(description = "清除cimon010订单、保单、交易记录")
    public void DEL_ORDER_APP_DATA() throws Exception{

    }


}
